/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.itip.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.itip.finder.Finder;
import com.sinodata.bsm.itip.tivoli.ConfManager;
import com.sinodata.bsm.itip.util.DB2ConnectionUtil;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author tangli
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-22 上午9:38:53          tangli         1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ResFindService {
    private static Logger logger = Logger.getLogger(ResFindService.class);

    /**
     * 
     */
    public ResFindService() {

    }

    /**
     * 资源发现的service
     * @param ip 要发现的资源IP
     * @param params 资源发现的参数配置
     * @param className 资源发现的类名
     * @return
     */
    @SuppressWarnings("rawtypes")
    public List<String[]> find(String ip, HashMap params, String className) {
        try {
            Finder finder = (Finder) Class.forName(className).newInstance();
            return finder.find(ip, params);
        } catch (Exception e) {
            logger.error("failed to find res,ip=" + ip + ",className=" + className);
            logger.error(e);
        }
        return null;
    }

    /**
     * 从 Tivoli获取特定顶级类别的资源列表
     * @param resType
     * @return
     */
    public List<String[]> getTypeResList(String resType) {
        System.out.println("to do type find--" + resType);
        List<String[]> list = new ArrayList<String[]>();
        Connection conn = null;
        String sql = "";
        Statement statement = null;
        ResultSet result = null;
        sql = ConfManager.getInstance().sqlMap.get(resType);
        //网络设备从mysql数据库中获取
        if ("2104120000000".equals(resType)) {
            conn = DB2ConnectionUtil.getInstance().getMysqlConnection();
        } else {
            conn = DB2ConnectionUtil.getInstance().getConnection();
        }
        try {
            statement = conn.createStatement();
            logger.info("[TIP]\tExecute Sql =" + sql);
            result = statement.executeQuery(sql);
            while (result.next()) {
                String[] stry = new String[1];
                stry[0] = result.getString(1).trim();
                list.add(stry);
            }
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
        } finally {
            if (result != null) {
                try {
                    result.close();
                } catch (SQLException ex) {
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException ex) {
                }
            }
            DB2ConnectionUtil.getInstance().returnConn(conn);
        }

        return list;
    }
}
